Previous Book Contents Book Index Next

Inside Macintosh: Programming With JManager /
Chapter 1 - Using JManager / Handling Events


Keyboard Events

Keyboard events occur whenever the user presses a key. These keypresses may correspond to text entered into a window, a keyboard-equivalent menu selection, or a similar action (for example, selecting the default button in a dialog box by pressing the return key). If the keyboard event occurs in a window that corresponds to a frame, you must pass the event to the frame using either the JMFrameKey function (page 86) for key-down events or the JMFrameKeyRelease function (page 87) for key-up events.

Listing 1-16 shows a simple example that handles a keyboard event.

Listing 1-16 Handling a keyboard event

static void handleKey(const EventRecord* eve)
{
   WindowPtr win;
   JMFrameRef frame;
   
   /* see if a menu item was selected */
   if (eve->what == keyDown && (eve->modifiers & cmdKey) == cmdKey) {
      long menuResult = MenuKey(eve->message & charCodeMask);
      if (menuResult != 0) {
         menuHit(menuResult >> 16, menuResult & 0xffff);
         return;
      }
   }
   /* otherwise, just let JManager deal with it */
   win = FrontWindow();
   if (win) {
      frame = (JMFrameRef) GetWRefCon(win);
      if (frame)
         JMFrameKey(frame, eve->message & charCodeMask, 
            (eve->message & keyCodeMask) >> 8, eve->modifiers);
   }
}
This code first checks to see if the keyboard input was a keyboard equivalent for a menu item (for example, Command-Q for Quit). If so, then control passes to the menu-event routine (see Listing 1-17 (page 30) for an example of handling a menu selection). In all other cases, the keyboard input is passed to the frame corresponding to the window, and the Java program can then determine the appropriate response. The content of the keyboard input is determined from the event record (the EventRecord structure) returned by the Event Manager.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
23 APR 1997